const asyncRoute = [
  // {
  //   path: '/',
  //   // redirect: { name: 'project' }
  // },
  // {
  //   name: 'login',
  //   path: '/login',
  //   component: () => import('../page/Login'),
  //   meta: {
  //     needLogin: false,
  //     menuIndex: 'login'
  //   }
  // },
  // {
  //   name: 'home',
  //   path: '/home',
  //   component: () => import('../page/Home'),
  //   meta: {
  //     needLogin: true,
  //     menuIndex: 'home'
  //   }
  // },
  {
    name: 'project',
    path: '/project',
    component: () => import('@/page/Project'),
    redirect: {
      name: 'list'
    },
    meta: {
      needLogin: true,
      menuIndex: 'project',
      title: '项目管理',
      perms: 'bigdata:project:list'

    },
    children: [
      {
        name: 'list',
        path: 'list',
        component: () => import('@/page/Project/List'),
        meta: {
          perms: 'bigdata:project:list'
        }
      },
      {
        path: ':projectId',
        component: () => import('@/page/Project/Layout'),
        meta: {
          needLogin: true,
          menuIndex: 'project',
          title: '项目管理'
        },
        children: [
          {
            name: 'dataSource',
            path: 'dataSource',
            component: () => import('@/page/Project/Layout/DataSource'),
            meta: {
              needLogin: true,
              menuIndex: 'dataSource',
              title: '数据源',
              icon: 'project_menu_dataSource',
              perms: 'bigdata:project:source:list'
            }
          },
          {
            name: 'workFolder',
            path: 'workFolder',
            component: () => import('@/page/Workspace'),
            meta: {
              needLogin: true,
              menuIndex: 'workFolder',
              title: '工作区',
              icon: 'project_menu_workbench',
              perms: 'bigdata:project:work'
            }
          },

          {
            // name: 'dataQuery',
            path: '',
            component: () => import('@/page/Project/Layout/DataQuery'),
            meta: {
              needLogin: true,
              menuIndex: 'dataQuery',
              title: '数据查询',
              icon: 'project_menu_dataFind',
              perms: 'bigdata:project:dql:list'
            },
            children: [
              {
                name: 'apiList',
                path: 'apiList',
                component: () => import('@/page/Project/Layout/DataQuery/ApiList'),
                meta: {
                  perms: 'bigdata:project:dql:category:list',
                  needLogin: true,
                  menuIndex: 'apiList',
                  title: 'API管理'
                }
              },
              {
                name: 'apiMonitor',
                path: 'apiMonitor',
                component: () => import('@/page/Project/Layout/DataQuery/ApiMonitor'),
                meta: {
                  perms: 'bigdata:project:dql:monitor:list',
                  needLogin: true,
                  menuIndex: 'apiMonitor',
                  title: 'API监控'
                }
              }
            ]
          },
          {
            name: 'accessToken',
            path: 'accessToken',
            component: () => import('@/page/Project/Layout/AccessToken'),
            meta: {
              needLogin: true,
              menuIndex: 'accessToken',
              title: '接口令牌',
              icon: 'project_menu_token',
              perms: 'bigdata:project:third:client:list'
            }
          }
        ]
      }
    ]
  },
  {
    name: 'nodes',
    path: '/nodes',
    component: () => import('@/page/Nodes'),
    redirect: {
      name: 'innnerOpe'
    },
    meta: {
      needLogin: true,
      menuIndex: 'nodes',
      title: '算子管理',
      perms: 'bigdata:operator'
    },
    children: [
      {
        name: 'nodeLayout',
        path: 'nodeLayout',
        component: () => import('@/page/Nodes/Layout'),
        meta: {
          needLogin: true,
          menuIndex: 'nodeLayout',
          title: '算子管理'
        },
        children: [
          {
            name: 'innnerOpe',
            path: 'innnerOpe',
            component: () => import('@/page/Nodes/Inner'),
            meta: {
              needLogin: true,
              menuIndex: 'innnerOpe',
              title: '内置算子',
              icon: 'project_menu_token',
              perms: 'bigdata:operator:init:list'
            }
          },
          {
            name: 'definedOpe',
            path: 'definedOpe',
            component: () => import('@/page/Nodes/Defined'),
            meta: {
              needLogin: true,
              menuIndex: 'definedOpe',
              title: '自定义算子',
              icon: 'project_menu_token',
              perms: 'bigdata:operator:custom:list'
            }
          }
        ]
      }
    ]
  },
  {
    name: 'jobs',
    path: '/jobs',
    component: () => import('@/page/Jobs'),
    meta: {
      needLogin: true,
      menuIndex: 'jobs',
      title: '任务管理',
      perms: 'bigdata:job:list'
    }
  },
  {
    name: 'schedules',
    path: '/schedules',
    component: () => import('@/page/Schedules'),
    meta: {
      needLogin: true,
      menuIndex: 'schedules',
      title: '调度管理',
      perms: 'bigdata:schedule:list'
    }
  },

  {
    name: 'resource',
    path: '/resource',
    component: () => import('@/page/Resource'),
    meta: {
      needLogin: true,
      menuIndex: 'resource',
      title: '资源管理',
      perms: 'bigdata:resource:list'
    }
  },
  // {
  //   name: 'systemSetting',
  //   path: '/systemSetting',
  //   component: () => import('@/page/SystemSetting'),
  //   meta: {
  //     needLogin: true,
  //     menuIndex: 'systemSetting',
  //     title: '系统设置',
  //     perms: 'bigdata:system'
  //   }
  // }
]
// import testAuth from './testAuth.json'

export function filterPermissons(allowAuth) {
  let allAuth = flatTree(allowAuth).map(i => i.perms)
  // flat auth 
  // flat routes
  return allAuth
}

export function filterRoutes(allowAuth) {
  let allAuth = flatTree(allowAuth).map(i => i.perms)
  // flat auth 
  // flat routes
  let routes = filterAsyncRoutes(asyncRoute, allAuth)
  return routes
}


const filterAsyncRoutes = (routes, roles) => {
  const res = []
  routes.forEach(route => {
    const tempRoute = { ...route }
    if (hasPermission(roles, tempRoute)) {
      if (tempRoute.children) {
        tempRoute.children = filterAsyncRoutes(tempRoute.children, roles)
      }
      res.push(tempRoute)
    }
  })
  return res
}


const hasPermission = (roles, route) => {
  const routeRoles = route.meta?.perms
  return routeRoles ? roles.some(role => routeRoles.includes(role)) : true
}

const flatTree = (arr, result = []) => {
  arr.forEach(item => {
    result.push(item)
    item.children && flatTree(item.children, result)
  })
  return result
}